#include "CTween.h"

Int32 CALLBACKTYPE GetExponentialValue(
    /* [in] */ Int32 fromValue,
    /* [in] */ Int32 toValue,
    /* [in] */ Millisecond32 elaspedTime,
    /* [in] */ Millisecond32 durationTime,
    /* [in] */ EaseType easeType)
{
    double dx = (double)(toValue - fromValue);
    double delta = 0;
    double ratio = ((double)elaspedTime) / durationTime;

    switch (easeType) {
    case EaseType_In:
        if (0 == elaspedTime) {
            break;
        }

        delta = dx * (pow(2, 10 * (ratio - 1)) - 0.001);

        break;

    case EaseType_Out:
        if (elaspedTime == durationTime) {
            delta = dx;
            break;
        }

        delta = dx * (-pow(2, -10 * ratio) + 1);

        break;

    case EaseType_InOut:
        if (0 == elaspedTime) {
            break;
        }

        if (elaspedTime == durationTime) {
            delta = dx;
            break;
        }

        ratio *= 2.0;

        if ((elaspedTime << 1) < durationTime) {
            delta = dx * 0.5 * pow(2, 10 * (ratio - 1));
        }
        else {
            delta = dx * 0.5 * (2 - pow(2, -10 * (ratio - 1)));
        }

        break;

    default:
        assert(0);
    }

    return (Int32)(fromValue + delta);
}
